package com.kgc.sbt.listener;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.kgc.sbt.api.GoodsApi;
import com.kgc.sbt.api.OrderInfoApi;
import com.kgc.sbt.api.VipUserApi;
import com.kgc.sbt.constant.ActiveMqConstant;
import com.kgc.sbt.domain.OrderItem;
import lombok.extern.slf4j.Slf4j;
import org.springframework.jms.annotation.JmsListener;
import org.springframework.messaging.handler.annotation.SendTo;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created on 2021/6/25.
 * <p>
 * Author: GongMingJie
 * <p>
 * Description:
 */
@Slf4j
@Component
public class PayListener {

    @Reference
    private GoodsApi goodsApi;

    @Reference
    private OrderInfoApi orderInfoApi;

    @Reference
    private VipUserApi vipUserApi;

    /**
     * Created on 2021/6/25
     * Author: GongMingJie
     * @param
     * @return
     * Description: 减库存
     */
    @JmsListener(destination = ActiveMqConstant.GOODS_ORDER_STOCK_MQ)
    public void receiveMessageFromQueueStock(String message){
        log.info("====== 3 从消息队列:{}，获取一条消息:{},begin ======", ActiveMqConstant.GOODS_ORDER_STOCK_MQ,message);
        // 减库存
        Boolean result = true;
        // ResumeHrSearchDTO resumeHrSearchDTO = JSONObject.parseObject(json, ResumeHrSearchDTO.class);
        Map<String,Object> map = JSONObject.parseObject(message,Map.class);
        List<OrderItem> orderItems = JSON.parseArray((String) map.get("order_items"),OrderItem.class);
        log.info("------ 接收到的orderItems{}",orderItems);
        for (OrderItem orderItem : orderItems){
            if (!goodsApi.pullStockById(orderItem.getGoodsId(),orderItem.getGoodsNum())){
                log.error("------减库存出错了");
                result = false;
            }
        }

        log.info("====== 3 从消息队列:{}，获取一条消息:{},end ======",ActiveMqConstant.GOODS_ORDER_STOCK_MQ,message);

        log.info("------ 减库存结果：{}",result);
    }

    @JmsListener(destination = ActiveMqConstant.GOODS_ORDER_STATUS_MQ)
    public void receiveMessageFromQueueStatus(String  message){
        log.info("====== 3 从消息队列:{}，获取一条消息:{},begin ======",ActiveMqConstant.GOODS_ORDER_STATUS_MQ,message);
        // 修改订单状态
        Map<String,Object> map1 = new HashMap<>();
        map1 = (Map<String,Object>)JSONObject.parse(message);

        Boolean result = orderInfoApi.modOrderStatus(map1.get("orderNumber").toString(),2);

        log.info("====== 3 从消息队列:{}，获取一条消息:{},end ======",ActiveMqConstant.GOODS_ORDER_STATUS_MQ,message);

        log.info("------ 修改订单状态结果：{}",result);
    }

    /**
     * Created on 2021/6/25
     * Author: GongMingJie
     * @param
     * @return
     * Description: 支付成功后处理加积分
     */
    @JmsListener(destination = ActiveMqConstant.GOODS_ORDER_SCORE_MQ)
    public void receiveMessageFromQueueScore(String message){
        log.info("====== 3 从消息队列:{}，获取一条消息:{},begin ======",ActiveMqConstant.GOODS_ORDER_SCORE_MQ,message);
        // 加积分
        Map<String,Object> map1 = new HashMap<>();
        map1 = (Map<String,Object>) JSONObject.parse(message);

        Boolean result = vipUserApi.addScore(Integer.valueOf(map1.get("userId").toString()), Integer.valueOf(map1.get("price").toString()));
        log.info("====== 3 从消息队列:{}，获取一条消息:{},end ======",ActiveMqConstant.GOODS_ORDER_SCORE_MQ,message);

        log.info("------ 加积分结果：{}",result);

    }
}
